<?php

namespace Home\Controller;

/**
 * 用户管理接口.
 */
class UserController extends CommonController
{
    //系统用户登陆后跳转到此处
    public function index()
    {
        $map['uid'] = session(C('USER_AUTH_KEY'));
        session('info', $this->getUserInfo($map['uid']));
        $data = session('info');
        if (!session(C('ADMIN_AUTH_KEY'))) {
            $data['create_time'] = time();
            $data['log'] = '于'.date('Y-m-d H:i:s').'登录系统';
            $this->addLog($data);
        }
        $this->assign('userName', session('user_name'));
        $this->assign('superadmin', session(C('ADMIN_AUTH_KEY')));
        $this->display();
    }

    //修改用户信息页面
    public function InfoMod()
    {
        $uid = I('id');
        $userinfo = M('userinfo');
        $result = $userinfo->where(array('uid' => $uid))->find();
        $this->assign('userName', session('user_name'));
        $this->assign('superadmin', session(C('ADMIN_AUTH_KEY')));
        $this->assign('uid', $uid);
        $this->assign('data', $result);
        $this->display();
    }

    //用户信息修改操作
    public function InfoModHandler()
    {
        if (IS_POST) {
            $userinfo = M('userinfo');
            $data = session('info');
            $true_name = $userinfo->where(array('uid' => I('uid')))->find()['true_name'];
            $data['log'] = '于'.date('Y-m-d H:i:s').'修改了用户'.$true_name.'信息';
            $data['create_time'] = time();
            if ($userinfo->where(array('uid' => I('uid')))->save($_POST)) {
                $this->addLog($data);
            //$this->success('修改成功','allUser?p='.session("p"),1);
            $this->success('修改成功', U('Home/User/allUser', array('p' => session(p))), 1);
            } else {
                //$this->error("没有信息被修改","InfoMod?id=".I("uid"));
            $this->success('没有信息被修改', U('Home/User/InfoMod', array('id' => I('uid'))), 1);
            }
        }
    }

    //用户删除操作
    public function infoDelete()
    {
        $user = M('User');
        $userinfo = M('userinfo');
        $role_user = M('role_user');
        $uid = I('get.id');
        $true_name = $userinfo->where(array('uid' => $uid))->find()['true_name'];
        $data = session('info');
        $data['log'] = '于'.date('Y-m-d H:i:s').'删除了用户'.$true_name;
        $data['create_time'] = time();
        $resultUser = $user->where(array('id' => $uid))->delete();
        $resultInfo = $userinfo->where(array('uid' => $uid))->delete();
        $resultRoleUser = $role_user->where(array('user_id' => $uid))->delete();
        if ($resultUser || $resultInfo || $resultRoleUser) {
            $this->addLog($data);
            //$this->success('用户删除完毕',"allUser?p=".session("p"),1);
        $this->success('删除成功', U('Home/User/allUser', array('p' => session(p))), 1);
        } else {
            //$this->error('用户未删除',"allUser?p=".session("p"),3);
        $this->error('用户未删除', U('Home/User/allUser', array('p' => session(p))), 3);
        }
    }

    //查看所有用户
    public function allUser()
    {
        session('p', I('p'));
        $this->assign('userName', session('user_name'));
        $this->fpage('userinfo', 50, '', array('work_id'));
        $this->assign('superadmin', session(C('ADMIN_AUTH_KEY')));
        $this->display('allUser');
    }

    //查看系统用户
    public function systemUser()
    {
        session('p', I('p'));
        $this->assign('userName', session('user_name'));
        $this->fpage('userinfo', 50, array('part' => 'system'), array('work_id'));
        $this->assign('superadmin', session(C('ADMIN_AUTH_KEY')));
        $this->display('systemUser');
    }

    //查看单位用户
    public function unitUser()
    {
        session('p', I('p'));
        $this->assign('userName', session('user_name'));
        $this->fpage('userinfo', 50, array('part' => 'unit'), array('work_id'));
        $this->assign('superadmin', session(C('ADMIN_AUTH_KEY')));
        $this->display('unitUser');
    }

    //用户操作记录
    public function userControl()
    {
        $log = M('log');
        $this->assign('userName', session('user_name'));
        $this->fpage('log', 50, '', array('create_time' => 'desc'));
        $this->assign('superadmin', session(C('ADMIN_AUTH_KEY')));
        $this->display('userControl');
    }
    //分页方法
    private function fpage($table, $listRows, $con, $order = '')
    {
        $user = M('user');
        $table = M($table);
        $count = $table->where($con)->count();
        $fpage = new \Think\Page($count, $listRows);
        $fpage->setConfig('header', '<li class="rows">共<b>%TOTAL_ROW%</b>条记录&nbsp;
              第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
        $fpage->setConfig('first', '首页');
        $fpage->setConfig('prev', '上一页');
        $fpage->setConfig('next', '下一页');
        $fpage->setConfig('last', '末页');
        $fpage->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER% ');
        $fpage->rollPage = 5;
        $infoData = $table->where($con)->limit($fpage->firstRow.','.$fpage->listRows)->order($order)->select();
        foreach ($infoData as $key => &$value) {
            if ($result = $user->where(array('id' => $value['uid']))->find()) {
                $value['user_name'] = $result['user_name'];
            }
        }
        $fpage->lastSuffix = false;
        $show = $fpage->show();
        $this->assign('count', $count);
        $this->assign('listRows', $fpage->listRows);
        $this->assign('infoData', $infoData);
        $this->assign('show', $show);
    }

    public function addLog($data)
    {
        $log = M('log');
        $resultAdd = $log->add($data);
    }

    private function getUserInfo($uid)
    {
        $userInfo = M('userinfo');
        $map['uid'] = $uid;
        $resultInfo = $userInfo->field('true_name,phone_num,company,depart,position,work_id')
                             ->where($map)->find();

        return $resultInfo;
    }
}
